package com.tsystems.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.validation.constraints.Future;
import javax.validation.constraints.NotNull;

@Entity
public class TimeTableElement implements Serializable {

	@Id
	private String id;

	@PrePersist
	public void prepareId() {
		if (id == null) {
			id = UUID.randomUUID().toString();
		}
	}

	private static final long serialVersionUID = -994487055812091568L;

	@NotNull(message = "Поезд должен быть задан.")
	@ManyToOne
	private Train train;

	@NotNull(message = "Станция отправления должна быть задана.")
	@ManyToOne
	private Station departureStation;

	@NotNull(message = "Станция прибытия должна быть задана.")
	@ManyToOne
	private Station arrivalStation;

	@NotNull(message = "Дата отправления должна быть задана.")
	@Future(message = "Дата отправления должна быть в будущем")
	private Date departureTime;

	@NotNull(message = "Дата прибытия должна быть задана.")
	@Future(message = "Дата прибытия должна быть в будущем")
	private Date arrivalTime;

	@OneToMany(mappedBy = "timeTableElement")
	private List<Ticket> tickets;

	public TimeTableElement() {
		this.tickets = new ArrayList<Ticket>();
	}

	public TimeTableElement(Train train, Station departureStation,
			Station arrivalStation, Date departureTime, Date arrivalTime) {
		this();
		this.train = train;
		this.departureStation = departureStation;
		this.arrivalStation = arrivalStation;
		this.departureTime = departureTime;
		this.arrivalTime = arrivalTime;
	}

	public Train getTrain() {
		return train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}

	public Station getDepartureStation() {
		return departureStation;
	}

	public void setDepartureStation(Station departureStation) {
		this.departureStation = departureStation;
	}

	public Station getArrivalStation() {
		return arrivalStation;
	}

	public void setArrivalStation(Station arrivalStation) {
		this.arrivalStation = arrivalStation;
	}

	public Date getDepartureTime() {
		return departureTime;
	}

	public void setDepartureTime(Date departureTime) {
		this.departureTime = departureTime;
	}

	public Date getArrivalTime() {
		return arrivalTime;
	}

	public void setArrivalTime(Date arrivalTime) {
		this.arrivalTime = arrivalTime;
	}

	public List<Ticket> getTickets() {
		return tickets;
	}

	public void setTickets(List<Ticket> tickets) {
		this.tickets = tickets;
	}

	@Override
	public String toString() {

		return train.toString() + "(" + departureStation.getName() + ", "
				+ arrivalStation.getName() + ")";
	}

}
